import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const {
    resourceId,
    userId,
    roleId,
    permissionType,
    pageNo = 1,
    pageSize = 10,
  } = query;

  // 资源权限数据
  const resourcePermissions = [
    // 酒店主楼资源权限
    {
      id: 1,
      resource_id: 1,
      user_id: 'admin',
      role_id: 'admin',
      permission_type: 'admin',
      can_copy: true,
      can_share: true,
      can_export: true,
      copy_scope: 'public',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 2,
      resource_id: 1,
      user_id: 'manager',
      role_id: 'manager',
      permission_type: 'write',
      can_copy: true,
      can_share: false,
      can_export: true,
      copy_scope: 'private',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    // 酒店管理系统资源权限
    {
      id: 3,
      resource_id: 10,
      user_id: 'admin',
      role_id: 'admin',
      permission_type: 'admin',
      can_copy: true,
      can_share: true,
      can_export: true,
      copy_scope: 'public',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 4,
      resource_id: 10,
      user_id: 'developer',
      role_id: 'developer',
      permission_type: 'read',
      can_copy: false,
      can_share: false,
      can_export: false,
      copy_scope: 'private',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    // 客房资源权限
    {
      id: 5,
      resource_id: 2,
      user_id: 'admin',
      role_id: 'admin',
      permission_type: 'admin',
      can_copy: true,
      can_share: true,
      can_export: true,
      copy_scope: 'public',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 6,
      resource_id: 2,
      user_id: 'housekeeper',
      role_id: 'housekeeper',
      permission_type: 'write',
      can_copy: false,
      can_share: false,
      can_export: false,
      copy_scope: 'private',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    // 前台资源权限
    {
      id: 7,
      resource_id: 3,
      user_id: 'admin',
      role_id: 'admin',
      permission_type: 'admin',
      can_copy: true,
      can_share: true,
      can_export: true,
      copy_scope: 'public',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 8,
      resource_id: 3,
      user_id: 'receptionist',
      role_id: 'receptionist',
      permission_type: 'write',
      can_copy: false,
      can_share: false,
      can_export: false,
      copy_scope: 'private',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    // 餐厅资源权限
    {
      id: 9,
      resource_id: 4,
      user_id: 'admin',
      role_id: 'admin',
      permission_type: 'admin',
      can_copy: true,
      can_share: true,
      can_export: true,
      copy_scope: 'public',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 10,
      resource_id: 4,
      user_id: 'waiter',
      role_id: 'waiter',
      permission_type: 'read',
      can_copy: false,
      can_share: false,
      can_export: false,
      copy_scope: 'private',
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
  ];

  // 过滤数据
  let filteredData = resourcePermissions.filter((item) => item.deleted === 0);

  if (resourceId) {
    filteredData = filteredData.filter(
      (item) => item.resource_id === Number(resourceId),
    );
  }

  if (userId) {
    filteredData = filteredData.filter((item) => item.user_id === userId);
  }

  if (roleId) {
    filteredData = filteredData.filter((item) => item.role_id === roleId);
  }

  if (permissionType) {
    filteredData = filteredData.filter(
      (item) => item.permission_type === permissionType,
    );
  }

  // 分页
  const startIndex = (Number(pageNo) - 1) * Number(pageSize);
  const endIndex = startIndex + Number(pageSize);
  const paginatedData = filteredData.slice(startIndex, endIndex);

  return {
    code: 0,
    message: '查询成功',
    data: {
      list: paginatedData,
      total: filteredData.length,
      pageNo: Number(pageNo),
      pageSize: Number(pageSize),
    },
  };
});
